{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "18e20fbc-056b-44ac-b1fc-2d34b8e99bcc",
   "metadata": {},
   "source": [
    "# Streaming for Chat Engine - Condense Question Mode"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1778820a-4c58-4b2f-8b1a-3d6f1f49995d",
   "metadata": {
    "tags": []
   },
   "source": [
    "Load documents, build the VectorStoreIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a9ac125a-79df-452d-9f58-ac4f30997acf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:numexpr.utils:Note: NumExpr detected 12 cores but \"NUMEXPR_MAX_THREADS\" not set, so enforcing safe limit of 8.\n",
      "Note: NumExpr detected 12 cores but \"NUMEXPR_MAX_THREADS\" not set, so enforcing safe limit of 8.\n",
      "INFO:numexpr.utils:NumExpr defaulting to 8 threads.\n",
      "NumExpr defaulting to 8 threads.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/suo/miniconda3/envs/llama/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.7) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
    "\n",
    "from llama_index import VectorStoreIndex, SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c4a6b55f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"../../data/paul_graham\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fa67c282",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorStoreIndex.from_documents(documents)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "63a4259d-89b5-49f8-b158-9eba5353d6f5",
   "metadata": {},
   "source": [
    "Chat with your data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "825b5bb3-37ff-4886-be2c-264584ca9eab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:llama_index.chat_engine.condense_question:Querying with: What was the next step in Paul Graham's career after his involvement with Y Combinator?\n",
      "Querying with: What was the next step in Paul Graham's career after his involvement with Y Combinator?\n"
     ]
    }
   ],
   "source": [
    "chat_engine = index.as_chat_engine(chat_mode=\"condense_question\", streaming=True)\n",
    "response_stream = chat_engine.chat(\"What did Paul Graham do after YC?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d8fa4310-4dc5-4787-a073-755d2e0b4887",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Paul Graham's next step in his career after his involvement with Y Combinator was to take up painting. He spent most of the rest of 2014 painting and then in March 2015 he started working on Lisp again.\n"
     ]
    }
   ],
   "source": [
    "response_stream.print_response_stream()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "67021e64-8665-4338-9fb4-c0f1d6361092",
   "metadata": {},
   "source": [
    "Ask a follow up question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f6181319-5d76-48c4-a5d4-23c6e9bc5ccb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:llama_index.chat_engine.condense_question:Querying with: What did Paul Graham do after he started working on Lisp again in March 2015?\n",
      "Querying with: What did Paul Graham do after he started working on Lisp again in March 2015?\n"
     ]
    }
   ],
   "source": [
    "response_stream = chat_engine.chat(\"What about after that?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "95045f5b-7964-4872-bc91-809d9debf1f5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Paul Graham spent the rest of 2015 writing essays and working on the new dialect of Lisp he called Arc. He also looked for an apartment to buy and started to plan a second still life painting from the same objects.\n"
     ]
    }
   ],
   "source": [
    "response_stream.print_response_stream()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "72cc02dd-90b7-4d63-bdb2-e4c4666f87ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:llama_index.chat_engine.condense_question:Querying with: What did Paul Graham do after he started working on the new dialect of Lisp he called Arc in 2015?\n",
      "Querying with: What did Paul Graham do after he started working on the new dialect of Lisp he called Arc in 2015?\n"
     ]
    }
   ],
   "source": [
    "response_stream = chat_engine.chat(\"Can you tell me more?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d4f8efbb-fcb0-4c58-b92b-d2264a7e7103",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Paul Graham worked on the new dialect of Lisp he called Arc for four years, from March 26, 2015 to October 12, 2019. During this time, he wrote the new Lisp, called Bel, in Arc. He also wrote essays and took his children to the coast on a sunny day in 2015. In the summer of 2016, he and his family moved to England. Finally, in the fall of 2019, he finished the project.\n"
     ]
    }
   ],
   "source": [
    "response_stream.print_response_stream()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c2c68de8-af58-4f7e-8759-19fc072873fd",
   "metadata": {},
   "source": [
    "Reset conversation state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d13cf082-1a91-43c5-8bad-76fa45be96f9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "chat_engine.reset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "627de435-d195-4dad-b314-a68e731979a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:llama_index.chat_engine.condense_question:Querying with: What happens after the current situation?\n",
      "Querying with: What happens after the current situation?\n"
     ]
    }
   ],
   "source": [
    "response_stream = chat_engine.chat(\"What about after that?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "75ef9e31-3cdb-4129-92f7-e61be201ea36",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "After the current situation, the narrator resumes painting and experimenting with a new kind of still life. He also resumes his old life in New York, now that he is rich. He is able to take taxis and eat in restaurants, which is exciting for a while. He also starts to connect with other people who are trying to paint in New York.\n"
     ]
    }
   ],
   "source": [
    "response_stream.print_response_stream()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
